using System;
using System.Configuration;

namespace Pegasus.Web.Security.SecureWebPage
{

    /// <summary>
    /// Represents an file entry in the &lt;secureWebPages&gt;
    /// configuration section.
    /// </summary>
    public class SecureWebPageDomainSetting : ConfigurationElement
    {

        #region Constructors

        /// <summary>
        /// Creates an instance of SecureWebPageFileSetting.
        /// </summary>
        public SecureWebPageDomainSetting()
            : base()
        {
        }

        /// <summary>
        /// Creates an instance with an initial encryptedUri value.
        /// </summary>
        /// <param name="encryptedUri">The domain for converting from an unencrypted site to an encrypted one.</param>
        public SecureWebPageDomainSetting(string encryptedUri)
            : base()
        {
        }

        /// <summary>
        /// Creates an instance with initial unencryptedUri values.
        /// </summary>
        /// <param name="encryptedUri">The domain for converting from an unencrypted site to an encrypted one.</param>
        /// <param name="unencryptedUri">The domain for converting from an encrypted site to an unencrypted one.</param>
        public SecureWebPageDomainSetting(string encryptedUri, string unencryptedUri)
            : base()
        {
        }

        #endregion

        /// <summary>
        /// Gets or sets the path of this file setting.
        /// </summary>
        [ConfigurationProperty("encryptedUri"), RegexStringValidator(@"^(?:|(?:https://)?[\w\-][\w\.\-]*(?:/[\w\.\-]+)*/?)$")]
        public string EncryptedUri
        {
            get { return (string)this["encryptedUri"]; }
            set
            {
                if (value != null && value.Length > 0)
                    this["encryptedUri"] = value;
                else
                    this["encryptedUri"] = null;
            }
        }


        /// <summary>
        /// Gets or sets the path to a URI for unencrypted redirections, if any.
        /// </summary>
        [ConfigurationProperty("unencryptedUri"), RegexStringValidator(@"^(?:|(?:http://)?[\w\-][\w\.\-]*(?:/[\w\.\-]+)*/?)$")]
        public string UnencryptedUri
        {
            get { return (string)this["unencryptedUri"]; }
            set
            {
                if (value != null && value.Length > 0)
                    this["unencryptedUri"] = value;
                else
                    this["unencryptedUri"] = null;
            }
        }

        /// <summary>
        /// Gets or sets the domain property for all cookies to ensure visibility of the cookies across secure/nonsecure domains.
        /// For example, if our secure domain is secure.mydomain.com and the nonsecure domain is www.mydomain.com, then we would set
        /// CookieDomain to .mydomain.com so that the cookies are visible to both secure and www.
        /// </summary>
        [ConfigurationProperty("cookieDomain")]
        public string CookieDomain
        {
            get { return (string)this["cookieDomain"]; }
            set
            {
                if (value != null && value.Length > 0)
                    this["cookieDomain"] = value;
                else
                    this["cookieDomain"] = null;
            }
        }
    }

    /// <summary>
    /// Represents a collection of SecureWebPageFileSetting objects.
    /// </summary>
    public class SecureWebPageDomainSettingCollection : ConfigurationElementCollection
    {

        /// <summary>
        /// Gets the element name for this collection.
        /// </summary>
        protected override string ElementName
        {
            get { return "domains"; }
        }

        /// <summary>
        /// Gets a flag indicating an exception should be thrown if a duplicate element 
        /// is added to the collection.
        /// </summary>
        protected override bool ThrowOnDuplicate
        {
            get { return true; }
        }

        /// <summary>
        /// Gets the element at the specified index.
        /// </summary>
        /// <param name="index">The index to retrieve the element from.</param>
        /// <returns>The SecureWebPageFileSetting located at the specified index.</returns>
        public SecureWebPageDomainSetting this[int index]
        {
            get { return (SecureWebPageDomainSetting)BaseGet(index); }
        }

        /// <summary>
        /// Creates a new element for this collection.
        /// </summary>
        /// <returns>A new instance of SecureWebPageFileSetting.</returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new SecureWebPageDomainSetting();
        }

		/// <summary>
		/// Gets the element key for a specified configuration element when overridden in a derived class.
		/// </summary>
		/// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"></see> to return the key for.</param>
		/// <returns>
		/// An <see cref="T:System.Object"></see> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"></see>.
		/// </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((SecureWebPageDomainSetting)element).UnencryptedUri;
        }

        /// <summary>
        /// Returns the index of a specified item in the collection.
        /// </summary>
        /// <param name="item">The item to find.</param>
        /// <returns>Returns the index of the item.</returns>
        public int IndexOf(SecureWebPageDomainSetting item)
        {
            if (item != null)
                return BaseIndexOf(item);
            else
                return -1;
        }
    }
}
